package net.tv90.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import net.tv90.business.dao.StudentMapper;
import net.tv90.business.dao.TeacherMapper;
import net.tv90.business.model.IdentifyInfo;
import net.tv90.business.model.domain.Student;
import net.tv90.business.model.domain.Teacher;
import net.tv90.business.model.dto.StudentIdentifyDTO;
import net.tv90.business.model.dto.TeacherIdentifyDTO;
import net.tv90.business.service.IdentifyService;
import net.tv90.business.util.CodeUtils;
import net.tv90.business.util.RedisUtils;
import net.tv90.business.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

@Slf4j
@Service
public class IdentifyServiceImpl implements IdentifyService {

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    TeacherMapper teacherMapper;

    String getCodeUrl = "http://localhost/verify/code";

    private final RestTemplate restTemplate;

    @Autowired
    RedisUtils redisUtils;

    public IdentifyServiceImpl(RestTemplateBuilder restTemplateBuilder) {
        this.restTemplate = restTemplateBuilder.build();
    }

    @Override
    public IdentifyInfo identifyTeacher(TeacherIdentifyDTO dto) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", dto.getCode());
        queryWrapper.eq("id_card", dto.getIdCard());
        queryWrapper.eq("real_name", dto.getRealName());
        Teacher teacher = teacherMapper.selectOne(queryWrapper);
        if (teacher != null) {
            redisUtils.setObject("identify::" + dto.getUsername(), teacher.getId());
            return new IdentifyInfo(dto.getUsername(), dto.getCode(), 1, teacher.getId());
        } else {
            return null;
        }
    }

    @Override
    public IdentifyInfo identifyStudent(StudentIdentifyDTO dto) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", dto.getCode());
        queryWrapper.eq("id_card", dto.getIdCard());
        queryWrapper.eq("real_name", dto.getRealName());
        Student student = studentMapper.selectOne(queryWrapper);
        if (student != null) {
            redisUtils.setObject("identify::" + dto.getUsername(), student.getId());
            return new IdentifyInfo(dto.getUsername(), dto.getCode(), 0, student.getId());
        } else {
            return null;
        }
    }

    @Override
    public Long getId(String username) {
        String key = "identify::" + username;
        if(redisUtils.hasKey(key)){
            return redisUtils.getObject(key);
        }

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(getCodeUrl)
                .queryParam("username", username);
        ResponseEntity<Result> responseEntity = restTemplate.getForEntity(builder.toUriString(), Result.class);
        if(responseEntity.getStatusCode() == HttpStatus.OK
                && responseEntity.getBody() != null) {
            String code = (String) responseEntity.getBody().getData();
            log.info("成功请求验证服务器，用户{}的学号/工号为{}", username, code);
            if(CodeUtils.codeIsStudentOrTeacher(code) == 1) {
                QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
                teacherQueryWrapper.eq("code", code);
                Teacher teacher = teacherMapper.selectOne(teacherQueryWrapper);
                if(teacher != null) {
                    Long id = teacher.getId();
                    redisUtils.setObject("identify::" + username, id);
                    return id;
                }
            } else if(CodeUtils.codeIsStudentOrTeacher(code) == 0) {
                QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
                studentQueryWrapper.eq("code", code);
                Student student = studentMapper.selectOne(studentQueryWrapper);
                if(student != null) {
                    Long id = student.getId();
                    redisUtils.setObject("identify::" + username, id);
                    return id;
                }
            }
        }

        return null;
    }

}
